Bot.retweet   A
last analyzed

Complexity

Conditions 2

Size

Total Lines 20
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 17
dl 0
loc 20
ccs 7
cts 7
cp 1
crap 2
rs 9.55
c 0
b 0
f 0
1 3
import Twit from 'twit';
2 3
import Debug from 'debug';
3
import Tweet, {TweetConfig} from './entities/Tweet';
4 3
import Helper from './Helper';
5 3
import Constant from './Constant';
6 3
import Like from './actions/Like';
7 3
import Retweet from './actions/Retweet';
8 3
import Search from './actions/Search';
9
10
export type BotConfig = {
11
    screenName: string,
12
    debugNamespace?: string,
13
    apiConfig: Twit.Options,
14
    tweetConfig: TweetConfig
15
};
16
17 3
export default class Bot {
18
    twit: Twit;
19
    config: BotConfig;
20
    debug: Debug.Debugger;
21
22
    constructor(config: BotConfig) {
23 9
        this.twit = new Twit(config.apiConfig);
24 9
        this.config = config;
25 9
        this.debug = Helper.objectExists(config.debugNamespace) && config.debugNamespace ? Debug(config.debugNamespace) : Debug(Constant.DEBUGGER_DEFAULT_NAMESPACE);
26
    }
27
28
    async retweet(searchParams: Twit.Params): Promise<void> {
29 1
        const like = new Like(this.config, this.twit, this.debug);
30 1
        const likeOnSuccess = async(tweet: Tweet) => await like.run({
31
            tweet
32
        });
33
34 1
        const retweet = new Retweet(this.config, this.twit, this.debug);
35 1
        const retweetOnSuccess = async(tweet: Tweet) => await retweet.run({
36
            tweet,
37
            onSuccess: likeOnSuccess
38
        });
39
40 1
        const search = new Search(this.config, this.twit, this.debug);
41 1
        await search.run({
42
            searchParams,
43
            onSuccess: retweetOnSuccess
44
        });
45
46 1
        this.debug('All done.');
47
    }
48
}